const storage = {
    set(key, value) {
        localStorage.setItem(key, JSON.stringify(value));
    },
    get(key) {
        return localStorage.getItem(key)?localStorage.getItem(key).replace('"','').replace('"',''):"";
    },
    getObj(key) {
        return localStorage.getItem(key)?JSON.parse(localStorage.getItem(key)):null;
    },
    remove(key) {
        localStorage.removeItem(key);
    },
    clear() {
        try {
            // 保存记住密码的凭据
            const rememberedUser = localStorage.getItem('rememberedUser');
            
            // 清除所有存储
            localStorage.clear();
            
            // 重新保存记住密码的凭据
            if (rememberedUser) {
                localStorage.setItem('rememberedUser', rememberedUser);
            }
        } catch (error) {
            console.error('清除存储时出错:', error);
        }
    },
    // 保存记住的用户信息
    saveRememberedUser(username, password, role) {
        const userData = {
            username,
            password,
            role,
            expireTime: Date.now() + (24 * 60 * 60 * 1000) // 24小时后过期
        };
        localStorage.setItem('rememberedUser', JSON.stringify(userData));
    },
    // 获取记住的用户信息
    getRememberedUser() {
        const rememberedUser = localStorage.getItem('rememberedUser');
        if (rememberedUser) {
            try {
                const userData = JSON.parse(rememberedUser);
                if (userData.expireTime && userData.expireTime > Date.now()) {
                    return userData;
                } else {
                    // 如果已过期，删除保存的凭据
                    localStorage.removeItem('rememberedUser');
                }
            } catch (e) {
                console.error('解析记住的用户数据失败', e);
                localStorage.removeItem('rememberedUser');
            }
        }
        return null;
    },
    // 删除记住的用户信息
    removeRememberedUser() {
        localStorage.removeItem('rememberedUser');
    }
}
export default storage;
